using System;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using UnityEngine;
using Valve.Newtonsoft.Json.Linq;

namespace CPRMulti.Tools
{
    public class SerializeObject
    {

        /// <summary>
        /// 将对象序列化为字节数组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte[] SerializeToBytes<T>(T obj)
        {
            byte[] retval = null;

            try
            {
                IFormatter formatter = new BinaryFormatter();
                using (MemoryStream stream = new MemoryStream())
                {
                    formatter.Serialize(stream, obj);
                    stream.Position = 0;
                    retval = new byte[stream.Length];
                    stream.Read(retval, 0, retval.Length);
                    stream.Flush();
                    stream.Close();
                }
            }
            catch(Exception e)
            {
                UnityEngine.Debug.LogError(e);
            }

            return retval;
        }

        /// <summary>
        /// 将字节数组反序列化为对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static T DesrializeByBytes<T>(byte[] bytes)
        {
            if (bytes == null || bytes.Length < 1)
                new NullReferenceException("bytes is null or bytes lenth is 0");

            T retval = default(T);
            try
            {
                IFormatter formatter = new BinaryFormatter();
                using (MemoryStream stream = new MemoryStream())
                {
                   
                    stream.Write(bytes, 0, bytes.Length);
                    stream.Position = 0;
                    UnityEngine.Debug.LogError(stream.Length);
                    retval = (T)formatter.Deserialize(stream);
                    stream.Flush();
                    stream.Close();
                }
            }
            catch (Exception e)
            {
                UnityEngine.Debug.LogError(e);
            }

            return retval;
        }

        /// <summary>
        /// 序列化一个类 不可以是数组形式的
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte[] UnitySerializedToBytes<T>(T obj)
        {
            byte[] retval = null;
            string str = JsonUtility.ToJson(obj);

            retval = Encoding.Unicode.GetBytes(str);
            return retval;
        }
        /// <summary>
        /// 反序列化一个类  不可以是数组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static T UnityDesrializeByBytes<T>(byte[] bytes)
        {
            string str = Encoding.Unicode.GetString(bytes);


            return JToken.Parse(str).ToObject<T>();
        }
    }
}
